package com.zhangtai.modules.service.impl.security;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.SecurityChangeDao;
import com.zhangtai.modules.dto.SecurityChangeDto;
import com.zhangtai.modules.entity.employee.PromotionEntity;
import com.zhangtai.modules.entity.security.SecurityChangeEntity;
import com.zhangtai.modules.service.security.SecurityChangeService;
import com.zhangtai.utils.Excel.EntityUtil;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
public class SecurityChangeServiceImpl extends ServiceImpl<SecurityChangeDao, SecurityChangeEntity> implements SecurityChangeService {

    @Autowired
    private SecurityChangeDao securityChangeDao;
    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<SecurityChangeEntity> page = this.page(
                new Page<SecurityChangeEntity>(pageNum,pageSize),
                new QueryWrapper<SecurityChangeEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }


    public PageResult<SecurityChangeDto> getChangeListAuth(@RequestParam(value = "securityId", required = false) Long securityId,
                                                       @RequestParam(value = "operationTime", required = false) String operationTime,
                                                       @RequestParam(value = "type", required = false) Integer type,
                                                       @RequestParam(value = "name", required = false) String name,
                                                       @RequestParam(value = "operateName", required = false) String operateName,
                                                       @RequestParam(value = "securityType", required = false) Integer securityType,
                                                       @RequestParam(value = "contractIds", required = false) List<Long> contractIds,
                                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        Page<SecurityChangeDto> page = new Page<>(pageNum,pageSize);
        List<SecurityChangeDto> list = securityChangeDao.getChangeListAuth(securityId,operationTime,type,name,operateName,securityType,contractIds,page);
        page.setRecords(list);
        return new PageResult<SecurityChangeDto>(page);
    }

    public PageResult<SecurityChangeDto> getChangeList(@RequestParam(value = "securityId", required = false) Long securityId,
                                                       @RequestParam(value = "operationTime", required = false) String operationTime,
                                                       @RequestParam(value = "type", required = false) Integer type,
                                                       @RequestParam(value = "name", required = false) String name,
                                                       @RequestParam(value = "operateName", required = false) String operateName,
                                                       @RequestParam(value = "securityType", required = false) Integer securityType,
                                                       @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        Page<SecurityChangeDto> page = new Page<>(pageNum,pageSize);
        List<SecurityChangeDto> list = securityChangeDao.getChangeList(securityId,operationTime,type,name,operateName,securityType,orgIds,page);
       page.setRecords(list);
       return new PageResult<SecurityChangeDto>(page);
    }

    @Override
    public SecurityChangeEntity getByIdE(Long id) {
            SecurityChangeEntity one = this.getOne(new QueryWrapper<SecurityChangeEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(SecurityChangeEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(SecurityChangeEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            SecurityChangeEntity entity = new SecurityChangeEntity();
        entity.setChangeId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}